import { Component } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import 'rxjs/add/operator/switchMap';
import { ViewDataService } from '../../service/viewData.service';
import { Pagination } from  '../../lib/pagination';

/* jquery插件 */
declare let $:any;

@Component({
    selector: 'modify_password',
    templateUrl: '../../../views/view_data/viewData.html',
    styleUrls: ['../../../css/view_data/viewData.css'],
    providers: [
        ViewDataService,
        Pagination
    ]
})

export class viewDataComponent {
    public statistics : any = {};
    public ranking : any = {};
    public envelope : any = {};
    public wechatGame:any = {};
    public month : any;
    public gid: any; //项目id
    private param : any = {};
    public selectedPage : any;
    public selectedPage3 : any;
    public count_tab:any;
    public asset_url:any;


    constructor(
        private router : Router,
        private route: ActivatedRoute,
        private viewData : ViewDataService,
        private Pagination : Pagination

    ){
        let self : any = this;
        console.log( self.route.params.value.id);
        self.gid = self.route.params.value.id;

        self.route.params.subscribe((params : any)=> {
            for (let key in params) {
                self.param[key] = params[key];
            }
        });
        if (self.param.page == undefined) {
            self.param.page = 1;
        }
        if (self.param.limit == undefined) {
            self.param.limit = 5;
        }
        self.StatisticsList(self.param.page);
        self.RankingList(self.param.page);
        // self.StatisticsList(self.param.page);
    }

    /**
     * 公众号的游戏data列表
     *
     * @param number: gid  项目ID
     * @param number: tab  1:访问量,2:口令红包,3:排行
     * @param number: page 分页ID
     * @param number: limit 显示记录数
     */
    public StatisticsList(){
        let self : any = this;
        let data : any = {
            id: self.gid,
            month : self.month,
            page: self.param.page,
            limit: self.param.limit
        };
        self.viewData.getGameStatisticsList({
            data: data,
            success: function (result : any) {
                console.log(result);
                self.asset_url = result.asset_url;
                self.statistics.list = result.data;
                self.wechatGame = result.wechat_mp_game_info;
                if (result.wechat_mp_game_info.status == 0){
                    self.wechatGame.status = "下线";
                }else {
                    self.wechatGame.status = "在线";
                }
                self.statistics.count = result.count;
                self.statistics.pagination = self.Pagination.getPage(self.param.page,result.page_count);
                console.log(self.statistics.pagination);
                self.selectedPage = self.statistics.pagination.current;
            }
        });
    }
    public RankingList(){
        let self : any = this;
        let data : any = {
            id: self.gid,
            page: self.param.page,
            limit: self.param.limit
        };
        self.viewData.getGameRankingList({
            data: data,
            success: function (result : any) {
                console.log(result);
                self.ranking.list = result.data;
                self.ranking.count = result.count;
                self.ranking.pagination = self.Pagination.getPage(self.param.page,result.page_count);
                console.log(self.ranking.pagination);
                self.selectedPage3 = self.ranking.pagination.current;
            }
        });
    }

    public previousPage(tab:any) {
        const current : number = parseInt(this.param.page);
        const page : number = this.Pagination.previousPage(current);
        this.param.page = page;
        this.router.navigate(['/view_data',this.gid,{page:page}]);
        switch (tab){
            case 1:
                this.selectedPage = page;
                this.StatisticsList();
                break;
            case 3:
                this.selectedPage3 = page;
                this.RankingList();
                break;
        }
    }

    public nextPage(tab:any) {
        switch (tab){
            case 1:
                this.count_tab = parseInt(this.statistics.pagination.count);
                break;
            case 3:
                this.count_tab = parseInt(this.ranking.pagination.count);
                break;
        }
        const current : number = parseInt(this.param.page);
        const page : number = this.Pagination.nextPage(current, this.count_tab);
        this.param.page = page;
            this.router.navigate(['/view_data',this.gid,{page:page}]);
        switch (tab){
            case 1:
                this.selectedPage = page;
                this.StatisticsList();
                break;
            case 3:
                this.selectedPage3 = page;
                this.RankingList();
                break;
        }
    }

    public locationPage(page : number,tab:any) {
        this.param.page = page;
        this.router.navigate(['/view_data',this.gid,{page:page}]);
        switch (tab){
            case 1:
                this.selectedPage = page;
                this.StatisticsList();
                break;
            case 3:
                this.selectedPage3 = page;
                this.RankingList();
                break;
        }
    }
}

